﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft;
using WsGameFrame;
using UnityEngine.UI;


public class AccountElement
{
    public string Key;
    public List<AccountItem> Accounts;
    public AccountElement(string key, List<AccountItem> items)
    {
        this.Key = key;
        this.Accounts = items;
    }
}

public class AccountItem {
    public string Account;
    public string PassWord;
    public AccountItem(string account,string password)
    {
        this.Account = account;
        this.PassWord = password;
    }
}

public class SaveFile : BaseSingltonMono<SaveFile> {

    public string SaveFilePath;

    public Dictionary<string, AccountElement> AccountDic;

    public InputField WriteKey;
    public InputField WriteAccount;
    public InputField WritePassword;

    public InputField ReadKey;

    
    private void Start()
    {
        SaveFilePath = string.Format("{0}/{1}", Application.persistentDataPath , "Save.dat");

        if (File.Exists(SaveFilePath))
        {
            ReadSave();
        }
        else
        {
            AccountDic = new Dictionary<string, AccountElement>();
        }

    }

    protected override void AwakeInit()
    {
       
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    public void OnExitClick() {
        Application.Quit();
    }

    void ReadSave()
    {
        string content = Filer.Singleton.ReadTXTFile(SaveFilePath);
        content = TransCoding.Singleton.Base64Decode(content);
        DebugHelper.Log("读取到的信息:" + content);
        NewtonJson<List<AccountElement>> newton = new NewtonJson<List<AccountElement>>(content);
        List<AccountElement> accountList = newton.GetEntityObject();

        //获取到数据
        //分到字典
        AccountDic = new Dictionary<string, AccountElement>();
        for (int i = 0; i < accountList.Count; i++)
        {
            AccountDic.Add(accountList[i].Key, accountList[i]);
        }

        //字典装载完毕

    }



    public void WriteSave()
    {
        List<AccountElement> alllist = new List<AccountElement>();
        foreach (var item in AccountDic) {
            alllist.Add(item.Value);
        }
        string json = NewtonJson<List<AccountElement>>.ToJson(alllist);
        json = TransCoding.Singleton.Base64Code(json);
        Filer.Singleton.WriteTXTFile(SaveFilePath, json);
    }

    /// <summary>
    /// 把Item装进有list的字典里
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="dic"></param>
    /// <param name="key"></param>
    /// <param name="item"></param>
    void AddItemIntoDicList<T>(Dictionary<string, List<T>> dic, string key, T item)
    {
        if (dic.ContainsKey(key))
        {
            dic[key].Add(item);
        }
        else
        {
            List<T> bases = new List<T>();
            bases.Add(item);
            dic.Add(key, bases);
        }
    }

    public void OnClickSave()
    {
        TitlieMgr.Singleton.Show("");
        if (WriteKey.text.Trim() == "")
        {
            WriteKey.text = "输入无效!"; return;
        }

        if (WriteAccount.text.Trim() == "")
        {
            WriteAccount.text = "输入无效!"; return;
        }

        if (WritePassword.text.Trim() == "")
        {
            WritePassword.text = "输入无效!"; return;
        }


        AccountItem account = new AccountItem(WriteAccount.text, WritePassword.text);

        if (AccountDic.ContainsKey(WriteKey.text))
        {
            AccountElement element = AccountDic[WriteKey.text];

            bool ismodify = false;
            for (int i = 0; i < element.Accounts.Count; i++)
            {
                if (element.Accounts[i].Account == WriteAccount.text)
                {
                    //里面包含此号码则属于修改
                    ismodify = true;
                    element.Accounts[i].PassWord = WritePassword.text;
                    TitlieMgr.Singleton.Show("修改存档成功!");
                }
            }

            if (!ismodify) //没有修改的情况则是以前没有的号码
            {
                //添加新的
                element.Accounts.Add(account);
                TitlieMgr.Singleton.Show("新建存档成功!");
            }
        }
        else
        {
            List<AccountItem> accountlist = new List<AccountItem>();
            accountlist.Add(account);

            AccountElement element = new AccountElement(WriteKey.text, accountlist);

            AccountDic.Add(WriteKey.text, element);
            TitlieMgr.Singleton.Show("新建组与存档成功!");

        }

        WriteSave();
    }

    public void OnClickGet()
    {
        TitlieMgr.Singleton.Show("");
        if (ReadKey.text.Trim() == "")
        {
            ReadKey.text = "输入无效!"; return;
        }

        if(AccountDic == null)
        {
            ReadKey.text = "数据字典为空!";
            return;
        }

        if (AccountDic.ContainsKey(ReadKey.text))
        {
            AccountElement element = AccountDic[ReadKey.text];
            NoticeMgr.Singleton.Show(element);

        }
        else
        {
            TitlieMgr.Singleton.Show("没有此数据!");
            NoticeMgr.Singleton.OnCloseClick();
        }
    }

    public void DeleteItem(string key,AccountItem item)
    {
        if (AccountDic.ContainsKey(key))
        {
            List<AccountItem> accounts = AccountDic[key].Accounts;
            accounts.Remove(item);
            if(accounts.Count == 0)
            {
                AccountDic.Remove(key);
            }
            WriteSave();
            OnClickGet();
        }
    }

}
